//
//  HMEmoticonKeyBoard.swift
//  Weibo
//
//  Created by Apple on 2017/7/17.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit

//  自定义表情键盘
/*
    1. 表情视图-> UICollectionView
    2. 页数指示器-> UIPageControl
    3. 表情视图工具栏-> UIStackView
 */
class HMEmoticonKeyBoard: UIView {
    
    //  MARK:   --懒加载控件
    //  表情视图
    fileprivate lazy var emoticonView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        let collectionView: UICollectionView = UICollectionView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: self.width, height: self.height - 35)), collectionViewLayout: flowLayout)
        collectionView.backgroundColor = self.backgroundColor
        //  注册cell
        collectionView.register(HMEmotionViewCell.self, forCellWithReuseIdentifier: EmoticonViewCellIdentifier)
        collectionView.dataSource = self
        collectionView.delegate = self
        //  cell的大小等于配图的大小
        flowLayout.itemSize = collectionView.size
        //  cell的间距
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        //  设置滚动方向
        flowLayout.scrollDirection = .horizontal
        //  去掉滚动指示器
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        //  开启分页
        collectionView.isPagingEnabled = true
        //  去掉弹簧效果
        collectionView.bounces = false
        
        return collectionView
    }()
    
    fileprivate lazy var pageControl: UIPageControl = {
        let pageCtr = UIPageControl()
        pageCtr.numberOfPages = 6
//        pageCtr.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
//        pageCtr.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        pageCtr.setValue(UIImage(named: "compose_keyboard_dot_selected")!, forKey: "_currentPageImage")
        pageCtr.setValue(UIImage(named: "compose_keyboard_dot_normal")!, forKey: "_pageImage")
        pageCtr.hidesForSinglePage = true
        return pageCtr
    }()
    
    //  表情工具栏
    fileprivate lazy var toolBar: HMEmoticonToolbar = HMEmoticonToolbar()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        
        //  runtime获取成员变量的名字和类型
//        var count: UInt32 = 0
//        let ivarList = class_copyIvarList(UIPageControl.self, &count)!
//        for i in 0..<count {
//            //  获取成员变量
//            let ivar = ivarList[Int(i)]!
//            //  获取成员变量的名字
//            let cname = ivar_getName(ivar)!
//            let name = String(cString: cname)
//            print(name)
//            let ctype = ivar_getTypeEncoding(ivar)!
//            let type = String(cString: ctype)
//            print(type)
//        }
        
        
        //  滚动到默认这组数据
        let normalIndexPath = IndexPath(item: 0, section: 1)
        emoticonView.scrollToItem(at: normalIndexPath, at: [], animated: false)
        setPageControlData(indexPath: normalIndexPath)
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addSubview(emoticonView)
        addSubview(pageControl)
        addSubview(toolBar)
        
        pageControl.snp.makeConstraints { (make) in
            make.bottom.equalTo(emoticonView)
            make.centerX.equalTo(emoticonView)
            make.height.equalTo(10)
        }
        toolBar.snp.makeConstraints { (make) in
            make.left.trailing.bottom.equalTo(self)
            make.height.equalTo(35)
        }
        
        toolBar.callBack = { [unowned self] (type) in
            let indexPath: IndexPath
            switch type {
            case .recent:
                print("最近")
                indexPath = IndexPath(item: 0, section: 0)
            case .normal:
                print("默认")
                indexPath = IndexPath(item: 0, section: 1)
            case .emoji:
                print("emoji")
                indexPath = IndexPath(item: 0, section: 2)
            case .lxh:
                print("浪小花")
                indexPath = IndexPath(item: 0, section: 3)
            }
            self.emoticonView.scrollToItem(at: indexPath, at: [], animated: false)
            
        }
    }
   
    //  设置页数控件数据
    fileprivate func setPageControlData(indexPath: IndexPath) {
        pageControl.numberOfPages = HMEmoticonTools.sharedTools.allEmoticonArray[indexPath.section].count
        pageControl.currentPage = indexPath.item
    }

    //  重新加载最近这组数据
    func reloadRecentData() {
        let indexPath = IndexPath(item: 0, section: 0)
        emoticonView.reloadItems(at: [indexPath])
    }
    
}

//  MARK:   --UICollectionViewDataSource
extension HMEmoticonKeyBoard: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return HMEmoticonTools.sharedTools.allEmoticonArray.count
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return HMEmoticonTools.sharedTools.allEmoticonArray[section].count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: EmoticonViewCellIdentifier, for: indexPath) as! HMEmotionViewCell
        cell.indexPath = indexPath
        cell.emoticonArray = HMEmoticonTools.sharedTools.allEmoticonArray[indexPath.section][indexPath.item]
        return cell
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //  获取内容中心点
        let contentCenterX = scrollView.width * 0.5 + scrollView.contentOffset.x
        let contentCenterY = scrollView.height * 0.5 + scrollView.contentOffset.y
        let contentCenter = CGPoint(x: contentCenterX, y: contentCenterY)
        //  根据中心点获取indexPath
        if let indexPath = emoticonView.indexPathForItem(at: contentCenter) {
            //  获取section
            let section = indexPath.section
            //  根据section选中指定的表情按钮
            toolBar.selectEmotionButton(section: section)
            self.setPageControlData(indexPath: indexPath)
        }
        
    }
}

